﻿/*
 * 文件名称：
 * 程序员：huangsb
 * 创建时间：2008/06/10
 * 
 * 功能说明：模板管理
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Whir.Common;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_Template_TemplateList : System.Web.UI.Page
{
    protected string dir = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["quickdir"] != null)
        {
            string qdir = Request.QueryString["quickdir"].ToString();
            this.dir = Server.MapPath("~/UploadFiles/template/" + qdir);

            //如无些文件夹请创建
            if (qdir == "en")
            {
                if (!Directory.Exists(this.dir))
                {
                    Directory.CreateDirectory(this.dir);
                }
            }
        }
        else
        {
            if (Request.QueryString["dir"] != null)
                this.dir = Request.QueryString["dir"].ToString();
            else
                this.dir = Server.MapPath("~/UploadFiles/template");
        }

        //设置返回上一级按钮
        string[] dirs = this.dir.Split('\\');
        if (dirs[dirs.Length - 1].ToLower() == "template")
        {
            this.btnBack.Visible = false;
            this.btnBack.Enabled = false;
        }
        else
            this.btnBack.Enabled = true;

        if (!IsPostBack)
        {
            //判断是否删除操作
            if (Request.QueryString["del"] != null)
            {
                string del = Request.QueryString["del"].ToString();
                if (del.ToLower().IndexOf(".htm") == -1)
                    FileSystemObject.DeleteFolder(del);
                else
                    FileSystemObject.DeleteFile(del);

                Response.Redirect("TemplateList.aspx?dir=" + this.dir);
            }

            StartTable();
        }
    }

    #region 生成Table
    /// <summary>
    /// 初始化Table
    /// </summary>
    /// <param name="tb"></param>
    protected void StartTable()
    {
        this.tbTemplate.Rows.Clear();

        //首行
        TableRow tr = CreateTableRow(true);
        tr.Cells[0].Text = "名称";
        tr.Cells[0].Style.Value = "background-color:Silver;";

        tr.Cells[1].Text = "大小";
        tr.Cells[1].Style.Value = "background-color:Silver;";

        tr.Cells[2].Text = "类型";
        tr.Cells[2].Style.Value = "background-color:Silver;";

        tr.Cells[3].Text = "最后修改时间";
        tr.Cells[3].Style.Value = "background-color:Silver;";

        tr.Cells[4].Text = "管理操作";
        tr.Cells[4].Style.Value = "background-color:Silver;";

        this.tbTemplate.Rows.Add(tr);

        //内容行
        BrowseFolder(this.dir);
    }

    /// <summary>
    /// 创建空行
    /// </summary>
    /// <param name="isFirst">是否首行</param>
    /// <returns>TableRow</returns>
    protected TableRow CreateTableRow(bool isFirst)
    {
        TableRow tr = new TableRow();

        for (int i = 0; i < 5; i++)
        {
            TableCell tc = new TableCell();
            SetTableCellClass(tc, isFirst);

            tr.Cells.Add(tc);
        }

        return tr;
    }

    /// <summary>
    /// 设置样式
    /// </summary>
    /// <param name="tc">列</param>
    /// <param name="isFirst">是否首行</param>
    protected void SetTableCellClass(TableCell tc, bool isFirst)
    {
        //if (isFirst)
        //{
        //    tc.CssClass = "";
        //}
        //else
        //{
        //    tc.Height = 22;
        //    tc.BackColor = System.Drawing.Color.White;
        //}

        //tc.HorizontalAlign = HorizontalAlign.Center;
    }
    #endregion

    //创建内行[遍历文件夹一级目录]
    protected void BrowseFolder(string Dir)
    {
        string[] Dirs = Directory.GetFileSystemEntries(Dir);

        foreach (string d in Dirs)
        {
            if (!File.Exists(d))
            {
                //文件夹[优先显示]
                string[] names = d.Split('\\');
                if (names[names.Length - 1] == ".svn")
                    continue;

                TableRow tr = CreateTableRow(false);

                tr.Cells[0].HorizontalAlign = HorizontalAlign.Left;
                tr.Cells[0].Text = "&nbsp;<img src='" + Settings.GetString("SystemPath") + "Whir_System/Image/ModelIcon/closefolder.gif' />&nbsp;<a href='?dir=" + Server.UrlEncode(d) + "'>" + names[names.Length - 1] + "</a>";


                long fSize = FileSystemObject.FolderSize(d);
                if (fSize > 179 && fSize < 1024)
                    fSize = 1024;
                tr.Cells[1].Text = Convert.ToString(fSize / 1024) + " KB";

                tr.Cells[2].Text = "文件夹";

                tr.Cells[3].Text = StringHelper.GetFriendlyDateTime(FileSystemObject.FolderLastTime(d));


                if (names[names.Length - 1] != "bak" && names[names.Length - 1] != "example" && names[names.Length - 1] != "cn" && names[names.Length - 1] != "en" && names[names.Length - 1] != "file")
                    tr.Cells[4].Text = "<a onclick=\"var dir='" + Server.UrlEncode(d) + "';var isOk = window.showModalDialog('ResetName.aspx',dir,'dialogHeight:140px; dialogWidth:300px;dialogFullscreen=no;');if(isOk != null){window.location.reload();}\" stype=\"text-decoration:underline;\" href=\"javascript:void(0);\">重命名</a>&nbsp;|&nbsp;<a href=\"?dir=" + Server.UrlEncode(this.dir) + "&del=" + Server.UrlEncode(d) + "\" onclick=\"return confirm('确定要删除文件夹(" + names[names.Length - 1] + ")吗?');\">删除</a>";
                else
                    tr.Cells[4].Text = "无";

                this.tbTemplate.Rows.Add(tr);
            }
        }

        string filepath = "";
        foreach (string d in Dirs)
        {
            if (File.Exists(d))
            {
                //文件
                string[] names = d.Split('\\');
                FileInfo fileInfo = new FileInfo(d);

                TableRow tr = CreateTableRow(false);

                filepath = d.Substring(Server.MapPath("~/UploadFiles/template/").Length);
                tr.Cells[0].Text = "&nbsp;<img src='" + Settings.GetString("SystemPath") + "Whir_System/Image/ModelIcon/htmlFile.gif' />&nbsp;<a onclick=\"openWin('" + "TemplateLabel.aspx?dir=" + Server.UrlEncode(d) + "') \"href=\"Javascript:void(0);\">" + names[names.Length - 1] + "</a>";
                tr.Cells[0].HorizontalAlign = HorizontalAlign.Left;

                long fSize = (fileInfo.Length >= 1024) ? fileInfo.Length : 1024;
                tr.Cells[1].Text = Convert.ToString(fSize / 1024) + " KB";

                tr.Cells[2].Text = "html文件";
                tr.Cells[3].Text = StringHelper.GetFriendlyDateTime(fileInfo.LastWriteTime);
                tr.Cells[4].Text = "<a onclick=\"var dir='" + Server.UrlEncode(d) + "';var isOk = window.showModalDialog('ResetName.aspx',dir,'dialogHeight:140px; dialogWidth:300px;dialogFullscreen=no;');if(isOk != null){window.location.reload();}\" href=\"javascript:void(0);\">重命名</a>&nbsp;|&nbsp;<a href=\"?dir=" + Server.UrlEncode(this.dir) + "&del=" + Server.UrlEncode(d) + "\" onclick=\"return confirm('确定要删除文件(" + names[names.Length - 1] + ")吗?');\">删除</a>";

                this.tbTemplate.Rows.Add(tr);
            }
        }
    }

    //返回上一级
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string d = this.dir.Substring(0, this.dir.LastIndexOf('\\'));

        Response.Redirect("TemplateList.aspx?dir=" + d);
    }
}
